package cn.edu.lzu.service.impl;

import cn.edu.lzu.common.assist.DataTree;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.dao.ExamineItemTypeMapper;
import cn.edu.lzu.entity.ExamineItem;
import cn.edu.lzu.entity.ExamineItemType;
import cn.edu.lzu.service.ExamineItemTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @PackageName: cn.edu.lzu.service.impl
 * @ClassName: ExamineItemTypeServiceImpl
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2019/12/11 14:55
 * @Version: 1.0
 */
@Service
@Transactional
public class ExamineItemTypeServiceImpl implements ExamineItemTypeService {
    @Resource
    private ExamineItemTypeMapper examineItemTypeMapper;

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取‘体检项目类型’树型结构的数据
     * @Date: 2019/12/13 14:17
     * @Param: []
     * @Return: java.util.List<java.lang.Object>
     **/
    @Override
    public List<Object> getTypeTreeData(long unitId) {
        List<ExamineItemType> examineItemTypeList = examineItemTypeMapper.selFirstLevelItemType(unitId);
        List<Object> dataTreeList = new ArrayList<>();
        dataTreeList = getDataTreeList(examineItemTypeList);
        return dataTreeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据Id更新项目类型记录的itemTypeName
     * @Date: 2020/1/5 17:07
     * @Param: [dataTree]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult updateTreeNodeName(DataTree dataTree) {
        int result = examineItemTypeMapper.updTypeNameById(dataTree.getTitle(), dataTree.getId());
        ResponseResult responseResult = new ResponseResult();
        if (result == 1) {
            responseResult.setCode(1);
            responseResult.setMsg("节点名称更新成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("节点名称更新失败");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 添加一个新的‘体检项目类型’
     * @Date: 2020/1/3 17:45
     * @Param: [parentId, typeName]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult addTypeTreeNode(int parentId, String typeName) {
        //根据parentId获取父节点层级信息(关键是level信息）
        ExamineItemType parentExamineItemType = examineItemTypeMapper.selExamineItemTypeById(parentId);
        ExamineItemType examineItemType = new ExamineItemType();
        //新‘项目类型’的信息
        examineItemType.setLevel(parentExamineItemType.getLevel() + 1);
        examineItemType.setDeleted(false);
        examineItemType.setParentId(parentId);
        examineItemType.setTypeName(typeName);
        examineItemType.setUnitId(parentExamineItemType.getUnitId());
        //在数据库中写入新的记录
        int result = examineItemTypeMapper.insExamineItemType(examineItemType);
        ResponseResult responseResult = new ResponseResult();
        if (result == 1) {
            responseResult.setMsg("项目类型‘" + typeName + "’添加成功");
            responseResult.setCode(1);
        } else {
            responseResult.setMsg("项目类型‘" + typeName + "’添加失败");
            responseResult.setCode(0);
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据项目类型编号删除项目类型及其子类型
     * @Date: 2020/1/4 17:35
     * @Param: [itemTypeId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult deleteTypeTreeNodes(int itemTypeId) {
        //int result=examineItemTypeMapper.delExamineItemTypesById(itemTypeId);
        ExamineItemType examineItemType = examineItemTypeMapper.selExamineItemTypeById(itemTypeId);
        List examineItemTypeList = new ArrayList<ExamineItemType>();
        examineItemTypeList.add(examineItemType);
        int result = 0;
        try {
            deleteTypeById(examineItemTypeList);
            result = 1;

        } catch (Exception e) {
            result = 0;
        }
        ResponseResult responseResult = new ResponseResult();
        if (result > 0) {
            responseResult.setCode(1);
            responseResult.setMsg("项目类型删除成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("项目类型删除失败");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 添加体检项目根类型
     * @Date: 2020/1/5 19:55
     * @Param: [itemTypeTreeRoot]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult addItemTypeTreeRoot(String itemTypeTreeRoot, long unitId) {
        ResponseResult responseResult = new ResponseResult();
        int result = examineItemTypeMapper.insExamineItemTypeByNameAndUnitId(itemTypeTreeRoot, unitId);
        if (result == 1) {
            responseResult.setCode(1);
            responseResult.setMsg("体检项目根类型添加成功。");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("体检项目根类型添加失败。");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据examineItemType的id获取相应的examineItemType
     * @Date: 2020/3/20 22:35
     * @Param: [itemTypeId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult getItemTypeById(Integer itemTypeId) {
        ExamineItemType examineItemType=examineItemTypeMapper.selExamineItemTypeById(itemTypeId);
        ResponseResult responseResult=new ResponseResult();
        if(examineItemType!=null){
            responseResult.setCode(1);
            responseResult.setMsg(examineItemType.getTypeName());
        }else{
            responseResult.setCode(0);
            responseResult.setMsg("未知");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据单位代码unitId获取‘体检项目类型’中level==1的记录
     * @Date: 2020/1/8 19:49
     * @Param: []
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItemType>
     **/
    @Override
    public List<ExamineItemType> getItemTypeLevel1List(long unitId) {
        List<ExamineItemType> examineItemTypeList=examineItemTypeMapper.selAllLevelEqualsOne(unitId);
        return examineItemTypeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据unitId获取‘体检项目类型’中level==2的记录
     * @Date: 2020/1/10 22:41
     * @Param: [itemTypeId, unitId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItemType>
     **/
    @Override
    public List<ExamineItemType> getItemTypeLevel2List(int itemTypeId, long unitId) {
        List<ExamineItemType> examineItemTypeList=examineItemTypeMapper.selAllLevel2Types(itemTypeId, unitId);
        return examineItemTypeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据unitId获取‘体检项目类型’中level==3的记录
     * @Date: 2020/1/11 14:23
     * @Param: [itemTypeId, unitId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItemType>
     **/
    @Override
    public List<ExamineItemType> getItemTypeLevel3List(int itemTypeId, long unitId) {
        List<ExamineItemType> examineItemTypeList=examineItemTypeMapper.selAllLevel3Types(itemTypeId, unitId);
        return examineItemTypeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据unitId获取‘体检项目类型’中level==4的记录
     * @Date: 2020/1/11 14:26
     * @Param: [itemTypeId, unitId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItemType>
     **/
    @Override
    public List<ExamineItemType> getItemTypeLevel4List(int itemTypeId, long unitId) {
        List<ExamineItemType> examineItemTypeList=examineItemTypeMapper.selAllLevel4Types(itemTypeId, unitId);
        return examineItemTypeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据ID递归删除项目类型
     * @Date: 2020/1/4 17:48
     * @Param: [itemTypeId]
     * @Return: int
     **/
    private void deleteTypeById(List<ExamineItemType> itemTypeList) throws Exception {
        if (itemTypeList == null) {
            return;
        }
        int length = itemTypeList.size();
        for (int i = 0; i < length; i++) {
            //存放一个新节点的数据
            ExamineItemType examineItemType = itemTypeList.get(i);
            //删除该节点
            examineItemTypeMapper.delExamineItemTypesById(examineItemType.getItemTypeId());

            int parentId = examineItemType.getItemTypeId();
            //搜索该级的子树
            List<ExamineItemType> anotherExamineItemTypeList = examineItemTypeMapper.selItemTypesByParentId(parentId);
            //递归删除子节点
            deleteTypeById(anotherExamineItemTypeList);
        }
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据第一级节点使用递归的方法获取体检项目类型树形结构的数据
     * @Date: 2019/12/13 14:18
     * @Param: [itemTypeList]
     * @Return: java.util.List<java.lang.Object>
     **/
    private List<Object> getDataTreeList(List<ExamineItemType> itemTypeList) {
        //如果该数组为空，则返回null；这是这个递归的出口
        if (itemTypeList == null || itemTypeList.size() == 0) {
            return null;
        }
        //根据itemTypeList获取该级及其子级的数据
        List<Object> dataTreeList = new ArrayList<>();
        if (itemTypeList != null && itemTypeList.size() > 0) {
            int length = itemTypeList.size();
            for (int i = 0; i < length; i++) {
                //存放一个新节点的数据
                DataTree dataTree = new DataTree();
                ExamineItemType examineItemType = itemTypeList.get(i);
                dataTree.setTitle(examineItemType.getTypeName());
                dataTree.setId(examineItemType.getItemTypeId());
                dataTree.setSpread(true);
                int parentId = examineItemType.getItemTypeId();
                //搜索该级的子树
                List<ExamineItemType> anotherExamineItemTypeList = examineItemTypeMapper.selItemTypesByParentId(parentId);
                List<Object> anotherDataTreeList = getDataTreeList(anotherExamineItemTypeList);
                dataTree.setChildren(anotherDataTreeList);
                dataTree.setChildren(anotherDataTreeList);
                dataTreeList.add(dataTree);
            }
        }
        return dataTreeList;

    }
}
